package by.demchenko.sample.webapp.page.admin;

import by.demchenko.sample.datamodel.Course;
import by.demchenko.sample.datamodel.Schedule;
import by.demchenko.sample.datamodel.Schedule_;
import by.demchenko.sample.services.ScheduleService;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;
import java.util.Iterator;

@AuthorizeInstantiation("ADMIN")
public class SchedulePage extends BaseAdminPage {

    @Inject
    ScheduleService scheduleService;

    @Override
    protected void onInitialize() {
        super.onInitialize();
        add(new Label("headerTitle", getPageTitle()));
        add(new Label("subtitle", getString("label.schedule")));

        ScheduleDataProvider courseDataProvider = new ScheduleDataProvider();

        DataView<Schedule> dataView = new DataView<Schedule>("coursesList", courseDataProvider) {
            @Override
            protected void populateItem(Item<Schedule> item) {
                final Schedule schedule = item.getModelObject();
                item.add(new Label("course", schedule.getCourse()));
                item.add(new Label("startDate", schedule.getStartDate()));
                item.add(new Label("endDate", schedule.getEndDate()));
                item.add(new Label("total", schedule.getTotal()));
                item.add(new Label("registered", schedule.getRegistered()));
                item.add(new Label("status", schedule.getStatus()));

                item.add(new Link("editbutton") {
                    @Override
                    public void onClick() {
                        setResponsePage(new ScheduleEditPage(schedule));
                    }
                });
                item.add(new Link("deletebutton") {
                    @Override
                    public void onClick()
                    {
                        scheduleService.delete(schedule.getId());
                        setResponsePage(new SchedulePage());
                    }
                });
            }
        };

        dataView.setItemsPerPage(5);
        add(dataView);

        add(new PagingNavigator("navigator", dataView));

        add(new Link("newbutton") {
            @Override
            public void onClick() {
                setResponsePage(new CourseEditPage(new Course()));
            }
        });
    }

    private class ScheduleDataProvider extends SortableDataProvider<Schedule, SingularAttribute<Schedule, ?>> {

        public ScheduleDataProvider() {
            super();
            setSort(Schedule_.startDate, SortOrder.ASCENDING);
        }

        @Override
        public Iterator<? extends Schedule> iterator(long l, long l1) {
            SingularAttribute<Schedule, ?> sortParam = getSort().getProperty();
            SortOrder propertySortOrder = getSortState().getPropertySortOrder(sortParam);
            boolean ascending = SortOrder.ASCENDING.equals(propertySortOrder);
            return scheduleService.getAll().iterator();

        }

        @Override
        public long size() {
            return scheduleService.getCount();
        }

        @Override
        public IModel<Schedule> model(Schedule schedule) {
            return new CompoundPropertyModel<>(schedule);
        }
    }

    private IModel<String> getPageTitle() {
        return new Model<String>(getClass().getSimpleName());
    }
}
